package com.jimikongjian.service;

import com.jimikongjian.dao.IRoom;
import com.jimikongjian.dao.IRoomFollower;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.Room;
import com.jimikongjian.models.RoomFollower;
import com.jimikongjian.service.base.IRoomFollowerService;
import com.jimikongjian.service.base.IRoomService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by fuxiuyang on 17/2/26.
 */
@Service("roomService")
public class RoomService implements IRoomService {

    @Autowired
    @Qualifier("iRoomImpl")
    private IRoom iRoomImpl;

    @Autowired
    @Qualifier("iRoomFollowerImpl")
    private IRoomFollower iRoomFollowerImpl;

    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;

    @Autowired
    @Qualifier("mailService")
    private MailService mailService;


    @Autowired
    @Qualifier("roomFollowerService")
    private IRoomFollowerService roomFollowerService;

    /**
     * 创建一个新的空间
     *
     * @param room
     * @return
     */
    @Transactional
    public ResponseMessage createRoom(Room room) {
        if (isValid(room.getName()) && isValid(room.getCover())
                && room.getCreatorId() > 0 && mailService.isEmail(room.getCreatorEmail())
                && isValid(room.getCreatorNick()) && isValid(room.getSignature())
                && (room.getType() >= -1 && room.getType() <= 1) && room.getPeoples() == 1){
//            int id = iRoomImpl.insertRoom(room);
//            Room roomRlt =  iRoomImpl.insertRoom(room);
            iRoomImpl.insertRoom(room);
            // 这里将用户创建者的id加入用户关注表中
            if (room.getId() > 0){
                RoomFollower roomFollower = new RoomFollower();
                roomFollower.setRoomId(room.getId());
                roomFollower.setUserId(room.getCreatorId());
                iRoomFollowerImpl.insertRoomFollower(roomFollower);
                return responseMessageService.makeMessage(Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.SYSTEM_ERROR);

        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 获取所有的空间信息的列表
     *
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectRoom(int start, int length) {
        if (start >= 0 && length > 0){
            List<Room> roomList = iRoomImpl.getRoomList(start,length);
            return responseMessageService.makeMessage(roomList, Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过空间的id号查找空间
     *
     * @param id
     * @return
     */
    public ResponseMessage selectRoomById(int id) {
        if (id > 0){
            Room room = iRoomImpl.getRoomById(id);
            if ( room != null){
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过创建者的id查找空间
     *
     * @param creatorId
     * @param type
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectRoomByCreatorId(int creatorId, int type, int start, int length) {
        if (creatorId > 0 && start >= 0 && length > 0){
            if (type == -2){
                List room = iRoomImpl.getRoomListByCreatorId(creatorId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == -1){
                List room = iRoomImpl.getMyRoomListByCreatorId(creatorId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == 0){
                List room = iRoomImpl.getPublicRoomListByCreatorId(creatorId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == 1){
                List room = iRoomImpl.getPrivateRoomListByCreatorId(creatorId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }
        }

        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过空间的名称，查找空间
     *
     * @param name
     * @param type
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectRoomByName(String name, int type, int start, int length) {
        if (start >= 0 && length > 0){
            if (type == -2){
                List room = iRoomImpl.getRoomListByName(name, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == -1){
                List room = iRoomImpl.getMyRoomListByName(name, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == 0){
                List room = iRoomImpl.getPublicRoomListByName(name, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == 1){
                List room = iRoomImpl.getPrivateRoomListByName(name, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }
        }

        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过userId，查找该用户关注的空间
     *
     * @param userId
     * @param type
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectRoomByUserId(int userId, int type, int start, int length) {
        if (type == -99 && start == -99 && length == -99 & userId > 0){
            List room = iRoomImpl.getAllRoomListByUserId(userId);
            return responseMessageService.makeMessage(room, Message.SUCCESS);
        }
        if (userId > 0 && start >= 0 && length > 0){
            if (type == -2){
                List room = iRoomImpl.getRoomListByUserId(userId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == -1){
                List room = iRoomImpl.getMyRoomListByUserId(userId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == 0){
                List room = iRoomImpl.getPublicRoomListByUserId(userId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }else if (type == 1){
                List room = iRoomImpl.getPrivateRoomListByUserId(userId, start, length);
                return responseMessageService.makeMessage(room, Message.SUCCESS);
            }
        }
        return responseMessageService.makeMessage(Message.SUCCESS);
    }

    /**
     * 通过userId，查找该用户关注的空间
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseMessage selectRoomByUserId(int userId) {
        return selectRoomByUserId(userId, -99, -99, -99);
    }

    /**
     * 更新空间信息,更新之前，做一次验证，验证该用户，有没有更改空间的权限
     * @param room
     * @return
     */
    public ResponseMessage updateRoom(Room room) {
        if (room.getId() > 0){
//            数据库中查询相应的空间信息，查看该用户，有没有更改空间的权限
                iRoomImpl.updateRoom(room);
                return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 将空间的关注用户+1
     *
     * @param id
     */
    @Override
    public void updateAddPeoplesCount(int id) {
        if (id > 0){
            iRoomImpl.updateAddPeoplesCount(id);
        }
    }

    /**
     * 将空间的关注用户-1
     *
     * @param id
     */
    @Override
    public void updateSubPeoplesCount(int id) {
        if (id > 0){
            iRoomImpl.updateSubPeoplesCount(id);
        }
    }

    /**
     * 通过空间的id号，查找是否存在该空间
     *
     * @param id
     * @return
     */
    public boolean hasRoom(int id) {
        if (id > 0){
            if (iRoomImpl.getRoomById(id) != null){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断字符串是否有效
     * @param str
     * @return
     */
    public boolean isValid(String str){
        if (str != ""  && str != null){
            return true;
        }
        return false;
    }
}
